package com.kai.service.upload.controller;

import com.kai.service.data.Result;
import com.kai.service.data.request.upload.SingleFileUploadRequest;
import com.kai.service.data.request.upload.UploadTaskRequest;
import com.kai.service.data.response.upload.SingleFileUploadResponse;
import com.kai.service.data.response.upload.UploadTaskResponse;
import com.kai.service.upload.facade.UploadFacade;
import com.kai.service.util.ResultResponseUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/upload/task")
@Tag(name = "文件上传")
public class UploadController {

    @Autowired
    private UploadFacade uploadFacade;

    /**
     * 获取上传任务
     *
     * @param fileIdentifier 文件唯一标识（MD5）
     * @return
     */
    @GetMapping("/{fileIdentifier}")
    @Operation(summary = "获取上传任务", parameters = {@Parameter(name = "fileIdentifier", description = "文件唯一标识（MD5）")})
    public Result<UploadTaskResponse> getUploadTask(@PathVariable("fileIdentifier") String fileIdentifier) {
        return ResultResponseUtil.success(uploadFacade.getUploadTask(fileIdentifier));
    }

    /**
     * 创建分片上传任务
     *
     * @param request
     * @return
     */
    @PostMapping("/create")
    @Operation(summary = "创建分片上传任务")
    public Result<String> createMultipartUploadTask(@Valid @RequestBody UploadTaskRequest request) {
        return ResultResponseUtil.success(uploadFacade.createMultipartUploadTask(request));
    }

    /**
     * 获取预签名上传url
     *
     * @param fileIdentifier 文件唯一标识（MD5）
     * @param partNumber 分片数
     * @return
     */
    @GetMapping("/sign/url/{fileIdentifier}/{partNumber}")
    @Operation(summary = "获取预签名上传url", parameters = {@Parameter(name = "fileIdentifier", description = "文件唯一标识（MD5）"), @Parameter(name = "partNumber", description = "分片数")})
    public Result<String> getPreSignUploadUrl(@PathVariable("fileIdentifier") String fileIdentifier, @PathVariable("partNumber") Integer partNumber) {
        return ResultResponseUtil.success(uploadFacade.getPreSignUploadUrl(fileIdentifier, partNumber));
    }

    /**
     * 合并分片
     *
     * @param fileIdentifier 文件唯一标识（MD5）
     * @return
     */
    @PostMapping("/merge/part/{fileIdentifier}")
    @Operation(summary = "合并分片 ", parameters = {@Parameter(name = "fileIdentifier", description = "文件唯一标识（MD5）")})
    public Result<Boolean> mergePart(@PathVariable("fileIdentifier") String fileIdentifier) {
        return ResultResponseUtil.success(uploadFacade.mergePart(fileIdentifier));
    }

    /**
     * 单文件上传
     *
     * @param request
     * @return
     */
    @PostMapping("/single")
    @Operation(summary = "单文件上传")
    public Result<SingleFileUploadResponse> singleFileUpload(@Valid @RequestBody SingleFileUploadRequest request) {
        return ResultResponseUtil.success(uploadFacade.singleFileUpload(request));
    }
}
